package com.yanqu.road.server.manger.activity.foxcard;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.foxcard.config.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.foxcard.FoxCardConfig;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardGameLogic;
import com.yanqu.road.pb.activity.FoxCardProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.foxcard.FoxCardModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.pack.PackageHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FoxCardMgr extends TempMgr {
    private static FoxCardConfig config = null;


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivity() {
        getLogger().info("reload foxCard activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.FoxCard.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no foxCard Activity in show time");
            config = null;
        } else {
            ActivityInfo tempActivityInfo = openActivityInfoList.get(0);
            List<Integer> activityIdList = new ArrayList<>();
            int activityId = tempActivityInfo.getActivityId();
            activityIdList.add(activityId);
            Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityId);
            FoxCardConfig tempConfig = new FoxCardConfig(tempActivityInfo, activityConfigMap);

            config = tempConfig;
            try {
                Class<?> aClass = Class.forName(FoxCardGameLogic.class.getName());
            } catch (Exception e) {

            }
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(FoxCardModule.class).initSystemOpen();
        }

        getLogger().info("reload foxCard activity end");
    }

    public static FoxCardProto.FoxCardConfigSyncMsg.Builder parseConfigMsg(String language) {
        if (config == null) {
            return null;
        }

        FoxCardProto.FoxCardConfigSyncMsg.Builder configMsg = FoxCardProto.FoxCardConfigSyncMsg.newBuilder();
        configMsg.setActivityId(config.getActivityId());

        for (FoxCardLotteryConfig lotteryConfig : config.getLotteryConfigMap().values()) {
            FoxCardProto.FoxCardLotteryTemp.Builder lotteryTemp = FoxCardProto.FoxCardLotteryTemp.newBuilder();
            lotteryTemp.setId(lotteryConfig.getId());
            lotteryTemp.setQuality(lotteryConfig.getQuality());
            lotteryTemp.setPointParam(lotteryConfig.getPointParam());
            lotteryTemp.setScore(lotteryConfig.getScore());
            lotteryTemp.setWeight(lotteryConfig.getWeight());
            configMsg.addLotterys(lotteryTemp);
        }

        for (FoxCardLotterySettleConfig settleConfig : config.getLotterySettleConfigList()) {
            FoxCardProto.FoxCardLotterySettleTemp.Builder settleTemp = FoxCardProto.FoxCardLotterySettleTemp.newBuilder();
            settleTemp.setId(settleConfig.getId());
            settleTemp.setType(settleConfig.getType());
            settleTemp.setParam(settleConfig.getParam());
            settleTemp.setRollType(settleConfig.getRollType());
            settleTemp.setReward(settleConfig.getReward());
            settleTemp.setName(ServerLanguageMgr.getContent(settleConfig.getName(), language));
            settleTemp.setDesc(ServerLanguageMgr.getContent(settleConfig.getDesc(), language));
            configMsg.addLotterySettles(settleTemp);
        }

        for (FoxCardTreeLevelConfig levelConfig : config.getTreeLevelConfigList()) {
            FoxCardProto.FoxCardTreeLevelTemp.Builder levelTemp = FoxCardProto.FoxCardTreeLevelTemp.newBuilder();
            levelTemp.setLevel(levelConfig.getLevel());
            levelTemp.setParam(levelConfig.getParam());
            levelTemp.setExp(levelConfig.getExp());
            levelTemp.setRollConsume(levelConfig.getRollConsume());
            levelTemp.setInitialCard(levelConfig.getInitialCard());
            levelTemp.setComboLevel(levelConfig.getComboLevel());
            levelTemp.setCanSkipChapter(levelConfig.getCanSkipChapter());
            levelTemp.setBigReward(levelConfig.getBigReward());
            configMsg.addTreeLevels(levelTemp);
        }

        for (FoxCardLabelConfig labelConfig : config.getLabelConfigMap().values()) {
            FoxCardProto.FoxCardLabelTemp.Builder labelTemp = FoxCardProto.FoxCardLabelTemp.newBuilder();
            labelTemp.setId(labelConfig.getId());
            labelTemp.setName(ServerLanguageMgr.getContent(labelConfig.getName(), language));
            labelTemp.setType(labelConfig.getType());
            labelTemp.setWeight(labelConfig.getWeight());
            labelTemp.setParam(labelConfig.getParam());
            labelTemp.setDesc(ServerLanguageMgr.getContent(labelConfig.getDesc(), language));
            labelTemp.setShowParams(labelConfig.getShowParams());
            configMsg.addLabels(labelTemp);
        }

        for (FoxCardRoundConfig roundConfig : config.getRoundConfigMap().values()) {
            FoxCardProto.FoxCardRoundTemp.Builder roundTemp = FoxCardProto.FoxCardRoundTemp.newBuilder();
            roundTemp.setId(roundConfig.getId());
            roundTemp.setPassScore(roundConfig.getPassScore().toString());
            roundTemp.setIsLimit(roundConfig.getIsLimit());
            roundTemp.setReward(roundConfig.getReward());
            roundTemp.setAppearanceId(roundConfig.getAppearanceId());
            roundTemp.setGoldReward(roundConfig.getGoldReward());
            configMsg.addRounds(roundTemp);
        }

        for (FoxCardLimitConfig limitConfig : config.getLimitConfigMap().values()) {
            FoxCardProto.FoxCardLimitTemp.Builder limitTemp = FoxCardProto.FoxCardLimitTemp.newBuilder();
            limitTemp.setId(limitConfig.getId());
            limitTemp.setType(limitConfig.getType());
            limitTemp.setParam(limitConfig.getParam());
            limitTemp.setWeight(limitConfig.getWeight());
            limitTemp.setName(ServerLanguageMgr.getContent(limitConfig.getName(), language));
            limitTemp.setDesc(ServerLanguageMgr.getContent(limitConfig.getDesc(), language));
            limitTemp.setRoundLimit(limitConfig.getRoundLimit());
            limitTemp.setChapterLimit(limitConfig.getChapterLimit());
            configMsg.addLimits(limitTemp);
        }

        for (FoxCardChapterConfig chapterConfig : config.getChapterConfigMap().values()) {
            FoxCardProto.FoxCardChapterTemp.Builder chapterTemp = FoxCardProto.FoxCardChapterTemp.newBuilder();
            chapterTemp.setId(chapterConfig.getId());
            chapterTemp.setType(chapterConfig.getType());
            chapterTemp.setParam(chapterConfig.getParam());
            chapterTemp.setName(ServerLanguageMgr.getContent(chapterConfig.getName(), language));
            chapterTemp.setDesc(ServerLanguageMgr.getContent(chapterConfig.getDesc(), language));
            chapterTemp.setTreeLimit(chapterConfig.getTreeLimit());
            chapterTemp.setPlaceDesc(ServerLanguageMgr.getContent(chapterConfig.getPlaceDesc(), language));
            chapterTemp.setShowParams(chapterConfig.getShowParams());
            configMsg.addChapters(chapterTemp);
        }

        for (FoxCardEffectCardConfig cardConfig : config.getEffectCardConfigMap().values()) {
            FoxCardProto.FoxCardCardTemp.Builder cardTemp = FoxCardProto.FoxCardCardTemp.newBuilder();
            cardTemp.setId(cardConfig.getId());
            cardTemp.setBuild(cardConfig.getBuild());
            cardTemp.setQuality(cardConfig.getQuality());
            cardTemp.setType(cardConfig.getType());
            cardTemp.setParams(cardConfig.getParams());
            cardTemp.setUpgradeParams(cardConfig.getUpgradeParams());
            cardTemp.setCanUp(cardConfig.getCanUp());
            cardTemp.setPrice(cardConfig.getPrice());
            cardTemp.setAppearance(cardConfig.getAppearance());
            cardTemp.setIsInitial(cardConfig.getIsInitial());
            cardTemp.setName(ServerLanguageMgr.getContent(cardConfig.getName(), language));
            cardTemp.setDesc(ServerLanguageMgr.getContent(cardConfig.getDesc(), language));
            cardTemp.setShowParams(cardConfig.getShowParams());
            cardTemp.setShowUpgradeParams(cardConfig.getShowUpgradeParams());
            cardTemp.setCanCopy(cardConfig.getCanCopy());
            configMsg.addCards(cardTemp);
        }

        for (FoxCardMagicWeaponConfig magicWeaponConfig : config.getMagicWeaponConfigMap().values()) {
            FoxCardProto.FoxCardMagicWeaponTemp.Builder magicWeaponTemp = FoxCardProto.FoxCardMagicWeaponTemp.newBuilder();
            magicWeaponTemp.setId(magicWeaponConfig.getId());
            magicWeaponTemp.setInitialParam(magicWeaponConfig.getInitialParam());
            magicWeaponTemp.setParam(magicWeaponConfig.getParam());
            magicWeaponTemp.setName(ServerLanguageMgr.getContent(magicWeaponConfig.getName(), language));
            magicWeaponTemp.setDesc(ServerLanguageMgr.getContent(magicWeaponConfig.getDesc(), language));
            magicWeaponTemp.setBuildName(ServerLanguageMgr.getContent(magicWeaponConfig.getBuildName(), language));
            magicWeaponTemp.setBoxWeight(magicWeaponConfig.getBoxWeight());
            magicWeaponTemp.setWeight(magicWeaponConfig.getWeight());
            magicWeaponTemp.setGold(magicWeaponConfig.getGold());
            magicWeaponTemp.setShowParams(magicWeaponConfig.getShowParams());
            configMsg.addMagicWeapons(magicWeaponTemp);
        }

        for (FoxCardSpellConfig spellConfig : config.getSpellConfigMap().values()) {
            FoxCardProto.FoxCardSpellTemp.Builder spellTemp = FoxCardProto.FoxCardSpellTemp.newBuilder();
            spellTemp.setId(spellConfig.getId());
            spellTemp.setType(spellConfig.getType());
            spellTemp.setParam(spellConfig.getParam());
            spellTemp.setName(ServerLanguageMgr.getContent(spellConfig.getName(), language));
            spellTemp.setDesc(ServerLanguageMgr.getContent(spellConfig.getDesc(), language));
            spellTemp.setBoxWeight(spellConfig.getBoxWeight());
            spellTemp.setWeight(spellConfig.getWeight());
            spellTemp.setGold(spellConfig.getGold());
            spellTemp.setShowParams(spellConfig.getShowParams());
            configMsg.addSpells(spellTemp);
        }

        for (FoxCardSorceryConfig sorceryConfig : config.getSorceryConfigMap().values()) {
            FoxCardProto.FoxCardSorceryTemp.Builder sorceryTemp = FoxCardProto.FoxCardSorceryTemp.newBuilder();
            sorceryTemp.setId(sorceryConfig.getId());
            sorceryTemp.setType(sorceryConfig.getType());
            sorceryTemp.setParam(sorceryConfig.getParam());
            sorceryTemp.setName(ServerLanguageMgr.getContent(sorceryConfig.getName(), language));
            sorceryTemp.setDesc(ServerLanguageMgr.getContent(sorceryConfig.getDesc(), language));
            sorceryTemp.setBoxWeight(sorceryConfig.getBoxWeight());
            sorceryTemp.setWeight(sorceryConfig.getWeight());
            sorceryTemp.setGold(sorceryConfig.getGold());
            sorceryTemp.setShowParams(sorceryConfig.getShowParams());
            configMsg.addSorceries(sorceryTemp);
        }

        for (FoxCardCouponConfig couponConfig : config.getCouponConfigMap().values()) {
            FoxCardProto.FoxCardCouponTemp.Builder couponTemp = FoxCardProto.FoxCardCouponTemp.newBuilder();
            couponTemp.setId(couponConfig.getId());
            couponTemp.setType(couponConfig.getType());
            couponTemp.setParam(couponConfig.getParam());
            couponTemp.setUnlockPremise(couponConfig.getUnlockPremise());
            couponTemp.setName(ServerLanguageMgr.getContent(couponConfig.getName(), language));
            couponTemp.setDesc(ServerLanguageMgr.getContent(couponConfig.getDesc(), language));
            couponTemp.setWeight(couponConfig.getWeight());
            couponTemp.setShowParams(couponConfig.getShowParams());
            configMsg.addCoupons(couponTemp);
        }

        for (FoxCardShopConfig shopConfig : config.getShopConfigMap().values()) {
            FoxCardProto.FoxCardShopTemp.Builder shopTemp = FoxCardProto.FoxCardShopTemp.newBuilder();
            shopTemp.setId(shopConfig.getId());
            shopTemp.setPosition(shopConfig.getPosition());
            shopTemp.setType(shopConfig.getCardType());
            shopTemp.setProbability(shopConfig.getWeight());
            shopTemp.setGold(shopConfig.getGold());
            shopTemp.setParam(shopConfig.getParam());
            shopTemp.setName(ServerLanguageMgr.getContent(shopConfig.getName(), language));
            shopTemp.setDesc(ServerLanguageMgr.getContent(shopConfig.getDesc(), language));
            shopTemp.setRefresh(shopConfig.getRefresh());
            configMsg.addShops(shopTemp);
        }

        for (FoxCardBuffConfig buffConfig : config.getBuffConfigMap().values()) {
            FoxCardProto.FoxCardBuffTemp.Builder buffTemp = FoxCardProto.FoxCardBuffTemp.newBuilder();
            buffTemp.setId(buffConfig.getId());
            buffTemp.setSort(buffConfig.getSort());
            buffTemp.setLittleSort(buffConfig.getLittleSort());
            buffTemp.setSkillType(buffConfig.getSkillType());
            buffTemp.setParam(buffConfig.getParam());
            buffTemp.setWeight(buffConfig.getWeight());
            buffTemp.setName(ServerLanguageMgr.getContent(buffConfig.getName(), language));
            buffTemp.setDesc(ServerLanguageMgr.getContent(buffConfig.getDesc(), language));
            buffTemp.setGold(buffConfig.getGold());
            buffTemp.setShowParams(buffConfig.getShowParams());
            configMsg.addBuffs(buffTemp);
        }

        return configMsg;
    }

    public static FoxCardConfig getConfig() {
        return config;
    }

    public static FoxCardProto.FoxCardConfigSyncMsg.Builder getConfigMsg(String language) {
        if (config == null) {
            return null;
        }

        FoxCardProto.FoxCardConfigSyncMsg.Builder configMsg = config.getConfigMsg(language);
        if (configMsg == null) {
            synchronized (config) {
                configMsg = config.getConfigMsg(language);
                if (configMsg == null) {
                    configMsg = parseConfigMsg(language);
                    config.setConfigMsg(language, configMsg);
                }
            }
        }
        return configMsg;
    }

    public static int checkInShowTime(int activityId) {
        if (config == null || config.getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return ActivityMgr.activityInShowTime(config.getActivityInfo()) ? 0 : GameErrorCode.E_ACTIVITY_OVER_TIME;
    }

    public static int checkInTime(int activityId) {
        if (config == null || config.getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return ActivityMgr.activityInTime(config.getActivityInfo()) ? 0 : GameErrorCode.E_ACTIVITY_OVER_TIME;
    }
}
